import $ from 'zepto'

export default function (router) {
  router.map({
    '/': {
      component (resolve) {
        require(['./views/welcome'], resolve)
      }
    },
    '/home': {
      component (resolve) {
        require(['./views/home'], resolve)
      }
    },
    '/list': {
      component (resolve) {
        require(['./views/list'], resolve)
      }
    },
    '/rank': {
      component (resolve) {
        require(['./views/rank'], resolve)
      }
    },
    '/invite': {
      component (resolve) {
        require(['./views/invite'], resolve)
      }
    },
    '/tasks': {
      component (resolve) {
        require(['./views/tasks'], resolve)
      }
    },
    '/user': {
      component (resolve) {
        require(['./views/user'], resolve)
      }
    },
    '/user/tasks': {
      component (resolve) {
        require(['./views/user/tasks'], resolve)
      }
    },
    '/user/withdraw': {
      component (resolve) {
        require(['./views/user/withdraw'], resolve)
      }
    },
    '/user/profit': {
      component (resolve) {
        require(['./views/user/profit'], resolve)
      }
    },
    '/user/profit/record': {
      component (resolve) {
        require(['./views/user/profitRecord'], resolve)
      }
    },
    '/more': {
      component (resolve) {
        require(['./views/more'], resolve)
      }
    },
    '/more/feedback': {
      component (resolve) {
        require(['./views/more/feedback'], resolve)
      }
    },
    '/more/about': {
      component (resolve) {
        require(['./views/more/about'], resolve)
      }
    },
    '/study/menu': {
      component (resolve) {
        require(['./views/study/menu'], resolve)
      }
    },
    '/study/college': {
      component (resolve) {
        require(['./views/study/college'], resolve)
      }
    },
    '/study/info': {
      component (resolve) {
        require(['./views/study/info'], resolve)
      }
    },
    '/study/midinfo': {
      component (resolve) {
        require(['./views/study/midinfo'], resolve)
      }
    },
    '/study/school': {
      component (resolve) {
        require(['./views/study/school'], resolve)
      }
    },
    '/study/steps': {
      component (resolve) {
        require(['./views/study/steps'], resolve)
      }
    },
    '/study/material': {
      component (resolve) {
        require(['./views/study/material'], resolve)
      }
    },
    '/study/partjob': {
      component (resolve) {
        require(['./views/study/partjob'], resolve)
      }
    },
    '/study/jobinfo': {
      component (resolve) {
        require(['./views/study/jobinfo'], resolve)
      }
    },
    '/travel': {
      component (resolve) {
        require(['./views/travel'], resolve)
      }
    },
    '/news': {
      component (resolve) {
        require(['./views/news/news'], resolve)
      }
    },
    '/news/newsinfo': {
      component (resolve) {
        require(['./views/news/newsinfo'], resolve)
      }
    },
    '/travel/mytravel': {
      component (resolve) {
        require(['./views/travel/mytravel'], resolve)
      }
    },
    '/vip/menu': {
      component (resolve) {
        require(['./views/vip/menu'], resolve)
      }
    },
    '/updateprofile': {
      component (resolve) {
        require(['./views/user/updateprofile'], resolve)
      }
    },
    '/consult': {
      component (resolve) {
        require(['./views/consult'], resolve)
      }
    },
    '/travel/merger': {
      component (resolve) {
        require(['./views/travel/merger'], resolve)
      }
    },
    '/travel/merger/detail': {
      component (resolve) {
        require(['./views/travel/detail'], resolve)
      }
    },
    '/user/mycard': {
      component (resolve) {
        require(['./views/user/mycard'], resolve)
      }
    },
    '/user/profile': {
      component (resolve) {
        require(['./views/user/profile'], resolve)
      }
    },
    '/user/introduction': {
      component (resolve) {
        require(['./views/user/introduction'], resolve)
      }
    },
    '/user/history': {
      component (resolve) {
        require(['./views/user/history'], resolve)
      }
    },
    '/user/right': {
      component (resolve) {
        require(['./views/user/right'], resolve)
      }
    },
    '/user/userupgrade-1': {
      component (resolve) {
        require(['./views/user/userupgrade-1'], resolve)
      }
    },
    '/user/userupgrade-0': {
      component (resolve) {
        require(['./views/user/userupgrade-0'], resolve)
      }
    },
    '/user/userupgrade-2': {
      component (resolve) {
        require(['./views/user/userupgrade-2'], resolve)
      }
    },
    '/user/assist': {
      component (resolve) {
        require(['./views/user/assist'], resolve)
      }
    },
    '/user/pay': {
      component (resolve) {
        require(['./views/user/pay'], resolve)
      }
    },
    '/user/userupgrade-3': {
      component (resolve) {
        require(['./views/user/userupgrade-3'], resolve)
      }
    },
    '/travel/baoming1': {
      component (resolve) {
        require(['./views/travel/baoming'], resolve)
      }
    },
    '/travel/baoming2': {
      component (resolve) {
        require(['./views/travel/baoming'], resolve)
      }
    },
    '/travel/profile': {
      component (resolve) {
        require(['./views/travel/profile'], resolve)
      }
    },
    '/user/pay2': {
      component (resolve) {
        require(['./views/travel/pay2'], resolve)
      }
    },
    '/user/travelinfo': {
      component (resolve) {
        require(['./views/travel/travelinfo'], resolve)
      }
    }
  })

  router.beforeEach(({to, from, next, abort, redirect}) => {
    let toPath = to.path
    let fromPath = from.path
    console.log(`to: ${toPath} from: ${fromPath}`)
    if (to.path === '/user') {
      console.log('/user路径')
      abort()
      location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx33b73bd6220f28a4&redirect_uri=http%3a%2f%2fcreatgo.com%2foec%2f%23!%2fuser&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect'
    }
    if (to.path === '/travel') {
      console.log('/travel路径')
      abort()
      location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx33b73bd6220f28a4&redirect_uri=http%3a%2f%2fcreatgo.com%2foec%2f%23!%2ftravel&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect'
    }
    if (to.path.indexOf('/travel/baoming1') !== -1) {
      if ($.user.type === 0) {
        redirect('/travel/profile')
      }
      else {
        redirect('/travel/baoming2')
      }
    }
    if (toPath.replace(/[^/]/g, '').length > 1) {
      router.app.isIndex = false
    }
    else {
      let depath = toPath === '/' || toPath === '/invite' || toPath === '/rank'
      router.app.isIndex = depath ? 0 : 1
    }
    next()
  })

  router.afterEach(function ({to}) {
    console.log(`成功浏览到: ${to.path}`)
    $.refreshScroller()
  })
}
